package com.xiaohudie.controller;

import com.xiaohudie.pojo.AjaxResult;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Author xiao hu die
 * @Date 2022/3/10 23:19
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/putfile")
public class PutFileController {
  @PostMapping("/file")
    public AjaxResult file(MultipartFile[] multipartFile) throws IOException {
      for (MultipartFile file : multipartFile) {
          if (!file.isEmpty()){
              file.transferTo(new File("D:\\fileImage\\"+file.getOriginalFilename()));
               log.info(file.getOriginalFilename() +"的大小为"+String.valueOf(file.getSize()));
          }
      }
      return AjaxResult.success("上传成功文件数量为:"+multipartFile.length);
  }

  @PostMapping("/shibietupian")
    public AjaxResult shibietupian(MultipartFile multipartFile) {
      Tesseract tesseract = new Tesseract();
      tesseract.setDatapath("D:\\java图片代码\\Tess4J-3.2.1-src\\Tess4J\\tessdata");
      File file = (File) multipartFile;

      String s = null;
      try {
          s = tesseract.doOCR(file);
      } catch (TesseractException e) {
          log.error("工具包报错"+e);
          e.printStackTrace();
      }

      return AjaxResult.success(s);
  }
}
